import { getEnum } from '@/cadmin/components/select/enum'

const state = {
  mainBusiness: [],
  mainBusinessEnum: {},
}

const getters = {
  mainBusiness: state => state.mainBusiness,
  mainBusinessEnum: state => state.mainBusinessEnum,
}

const actions = {
  async getMainBusiness({ commit }, force = false) {
    if (!force && state.mainBusiness.length) {
      return Promise.resolve(state.mainBusiness)
    }

    const res = await getEnum('mainBusiness', { order_num: 'asc' })
    const data = (res && res.data) || []
    const enumData = (res && res.enum) || {}
    commit('mainBusiness', data)
    commit('mainBusinessEnum', enumData)
    return data
  },
}

const mutations = {
  mainBusiness: (state, payload) => (state.mainBusiness = payload),
  mainBusinessEnum: (state, payload) => (state.mainBusinessEnum = payload),
}

export default {
  state,
  getters,
  actions,
  mutations,
}
